/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.server;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import za.org.foobar.famon.frontend.client.dto.CModelObjectSuggestion;
import za.org.foobar.famon.model.ModelObject;
import za.org.foobar.famon.util.DB;

import java.util.List;


public class ModelObjectLogic {
    private final Log log = LogFactory.getLog(getClass());
   
    private static ModelObjectLogic instance;
    public static ModelObjectLogic get() {
        return instance == null ? instance = new ModelObjectLogic() : instance;
    }
              
    public ModelObjectLogic() {
        log.debug("Creating ModelObjectLogic");
    }

    public CModelObjectSuggestion[] getModelObjects(String dbClassName, String dbValueField,
                                                    String dbWhere, String query) {
        StringBuilder str = new StringBuilder();
        str.append("select id, ").append(dbValueField)
                .append(" from ").append(ModelObject.class.getPackage().getName())
                .append(".").append(dbClassName)
                .append(" where ").append(dbValueField).append(" >= searchQueryStringParam1")
                .append(" && ").append(dbValueField).append(" < searchQueryStringParam2");
        if (dbWhere != null && dbWhere.length() > 0) {
            str.append(" && ").append(dbWhere);
        }
        str.append(" parameters String searchQueryStringParam1, String searchQueryStringParam2")
                .append(" order by ").append(dbValueField).append(" asc");
        log.info("Querying: " + str.toString() + ". with query '" + query + "'");
        List<Object[]> resultList = (List<Object[]>)DB.createQuery(str.toString()).execute(query, query + (char)255);
        CModelObjectSuggestion[] suggestionList = new CModelObjectSuggestion[resultList.size()];
        int i = 0;
        for (Object[] result : resultList) {
            suggestionList[i] = new CModelObjectSuggestion(KeyFactory.keyToString((Key)result[0]), (String)result[1]);
            i++;
        }
        log.info("Returning " + suggestionList.length + " results");
        return suggestionList;
    }
}
